<?php
declare (strict_types = 1);
namespace app\index\model\user;

use think\Model;

/**
 * @mixin \think\Model
 */
class UserBill extends Model
{
    protected $pk = "bill_id";

    /**
     * 获取信息
     * @param $where
     * @return array|null|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function getInfoByWhere($where){
        return $this->where($where)->find();
    }

    /**
     * 添加记录
     * @param $userInfo
     * @param $number
     * @param $addData
     * @return int|string
     */
    function addRecord($userInfo,$number,$addData){
        $saveData = [
            'uid'=>$userInfo['id'],
            'link_id'=>isset($addData['link_id']) ? $addData['link_id'] : 0,
            'pm'=>$addData['pm'],
            'title'=>isset($addData['title']) ? $addData['title'] : '',
            'category'=>isset($addData['category']) ? $addData['category'] : '',
            'type'=>isset($addData['type']) ? $addData['type'] : '',
            'number'=>$number,
            'balance'=>$userInfo['now_money'],
            'mark'=>isset($addData['mark']) ? $addData['mark'] : '',
            'create_time'=>date("Y-m-d H:i:s"),
            'status'=>1,
        ];
        return $this->insertGetId($saveData);
    }

    /**
     * 用户账单列表
     * @param $where
     * @param int $page
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    function userBillList($where,$page = 1,$limit = 15){
        $count = $this->where($where)->count();
        $list = $this->where($where)->page($page,$limit)->order('bill_id','desc')->select();
        if($list) $list = $list->toArray();
        return compact('count','list');
    }

}
